/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.valarnin.plugins.citygen;

import com.valarnin.plugins.citygen.generator.CityGen;
import java.util.Random;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.generator.BlockPopulator;

/**
 *
 * @author valarnin
 */
public class CityGenPopulator extends BlockPopulator {
    
    //TODO: Move this out to a config file
    /**
     * Chunk count modifier. The higher this is, the more chunks are in a city.
     */
    private static final double CHUNK_COUNT_MODIFIER = 1;
    
    //TODO: Move this out to a config file
    /**
     * Minimum distance. The minimum distance between two cities.
     */
    private static final double MIN_DISTANCE = 100;
    
    //TODO: Move this out to a config file
    /**
     * City Odds. The odds that a city will get generated on a given chunk
     * after the chunk meets the MIN_DISTANCE requirement.
     */
    private static final double CITY_ODDS = 1;

    @Override
    public void populate(World world, Random random, Chunk chunk) {
        CityGen cityGen = CityGen.GetCityGenerator();
        
        int x = chunk.getX();
        int z = chunk.getZ();
        
        if(cityGen.isRequiredCityBlock(x, z)) {
            cityGen.addChunk(chunk, random);
        } else if(cityGen.isAdjacentToCity(x, z)) {
            int chunks = cityGen.getTotalChunksAdjacent(x, z);
            
            double modifiedChunks = (double)chunks * CHUNK_COUNT_MODIFIER;
            
            //Hard capped at 100%
            if(modifiedChunks >= 100) {
                return;
            }
            
            //The more chunks there are, the less chance of adding a chunk
            double chance = 100.0 - ((random.nextDouble() * 100.0) + modifiedChunks);
            
            if(chance < 100) {
                cityGen.addChunk(chunk, random);
            }
        } else {
            double distance = cityGen.getClosestCityDistance(x, z);
            if(distance > MIN_DISTANCE) {
                distance -= MIN_DISTANCE;
                distance *= CITY_ODDS;
                
                if(distance > (random.nextDouble() * 100.0)) {
                    cityGen.addCity(chunk, random);
                }
            }
        }
    }
    
}
